
package com.dps.omc.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.omc.model.TaskLog;
import com.dps.omc.service.ITaskLogService;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class TaskLogAction extends AbstractAdminController<ITaskLogService>{

    @Autowired
    private ITaskLogService taskLogService;
    
    @RequestMapping(value="/tasklog/list")
    public String list(Date lastModifiedTimeStart, Date lastModifiedTimeEnd, TaskLog taskLog, ModelMap model, PageParam pageParam, HttpServletRequest request, HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(taskLog);
        conditions.put("lastModifiedTimeStart",lastModifiedTimeStart);
        conditions.put("lastModifiedTimeEnd",lastModifiedTimeEnd);
    	BizData4Page<TaskLog> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("taskLog", taskLog);
        model.addAttribute("lastModifiedTimeStart", lastModifiedTimeStart);
        model.addAttribute("lastModifiedTimeEnd", lastModifiedTimeEnd);
    	return "/module/tasklog/list";
    }    
    
    @RequestMapping(value="/tasklog/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("taskLog", new TaskLog());
        return "module/tasklog/edit";
    }    
    
    @RequestMapping(value="/tasklog/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	TaskLog taskLog = taskLogService.fetch(id);
    	model.addAttribute("taskLog", taskLog);
        return "module/tasklog/edit";
    }

    @RequestMapping(value="/tasklog/track")
    public String track(String taskId,ModelMap model,HttpServletRequest request,HttpServletResponse response){

        Map<String,Object> conditions = new HashMap<>();
        conditions.put("taskId",taskId);
        List<TaskLog> rows = taskLogService.queryList(conditions,"createDate","asc");
        model.addAttribute("rows",rows);
        return "module/tasklog/track";
    }

    @RequestMapping(value="/tasklog/save")
    @ResponseBody
    public String save(HttpServletRequest request,TaskLog taskLog){
		boolean isAddNew = StringUtils.isBlank(taskLog.getId())?true:false;
		if(isAddNew)
		{
			taskLog.setId(IdGenerator.createNewId());
			taskLogService.add(taskLog);
		}
		else
		{
			taskLogService.update(taskLog);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/tasklog/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	taskLogService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }

    protected Map getQueryMap(TaskLog taskLog)
    {
        Map<String, Object> conditions = new HashMap();

        conditions.put("taskId", taskLog.getTaskId());
        conditions.put("agentId", taskLog.getAgentId());
        conditions.put("jobId", taskLog.getJobId());
        conditions.put("templateId", taskLog.getTemplateId());
        conditions.put("artifactId", taskLog.getArtifactId());
        conditions.put("clientIp", taskLog.getClientIp());
        conditions.put("returnMsg", taskLog.getReturnMsg());
        conditions.put("planDate", taskLog.getPlanDate());
        conditions.put("actualDate", taskLog.getActualDate());
//        conditions.put("isTimer", taskLog.getIsTimer());
        conditions.put("note", taskLog.getNote());
        conditions.put("status", taskLog.getStatus());
        conditions.put("startDate", taskLog.getStartDate());
        conditions.put("endDate", taskLog.getEndDate());
        conditions.put("createDate", taskLog.getCreateDate());
        conditions.put("createrId", taskLog.getCreaterId());
        conditions.put("createrName", taskLog.getCreaterName());
        conditions.put("lastModifiedTime",taskLog.getLastModifiedTime());
        conditions.put("lastModifiedId",taskLog.getLastModifiedId());
        conditions.put("lastModifiedName",taskLog.getLastModifiedName());
        return conditions;
    }

    @Override
    protected ITaskLogService getMainService() {
        return taskLogService;
    }

    @Override
    protected String getMainObjName() {
        return "tasklog";
    }

    @Override
    protected String getViewTitle() {
        return "tasklog";
    }
}
